from django.core.paginator import Paginator
from django.http import HttpResponseBadRequest

from backend.response_helper import to_response
from info.models import Repository, Commit, File

"""
GET /api/repo
[参数]
repo_id 仓库ID(由Load返回) [必须]
with_readme 是否携带README文件内容 [0不携带,其他值携带,默认0]
[返回]
# 其中各属性的含义见: info/models.py - Repository对象
{"status": 0/-1, "msg": "...", "payload": {"platform": "...", "name": "...", "readme": "..."}}
"""
def get_repo(request):
    repo_id = request.GET.get("repo_id")
    with_readme = request.GET.get("with_readme", 0)
    if repo_id is None:
        return to_response(status=-1, response_type=HttpResponseBadRequest, msg="请求参数错误")
    try:
        repo = Repository.objects.get(id=repo_id)
    except Repository.DoesNotExist:
        return to_response(status=-1, response_type=HttpResponseBadRequest, msg="无此仓库")
    readme = ""
    if with_readme != 0:
        readme = repo.readme
    return to_response(payload={
        "platform": repo.platform,
        "name": repo.name,
        "readme": readme,
    })


"""
GET /api/commit
[参数]
repo_id 仓库ID(由Load返回) [必须]
page 某页 [必须]
per_page 每页Commit数量 [默认20]
[返回]
{"status": 0/-1, "msg": "...", "payload": 
    {
        "has_next": true/false,
        "commits":[
        # 其中各属性的含义见: info/models.py - Commit对象
        {"hash": "...", "content": "...", "time": "%Y-%m-%d %H:%M:%S", "status": 0, "result_sa": "...", "result_nlp": "..."},
        {"hash": "...", "content": "...", "time": "%Y-%m-%d %H:%M:%S", "status": 0, "result_sa": "...", "result_nlp": "..."},
        ...
        ]
    }

}
"""
def get_commit(request):
    repo_id = request.GET.get("repo_id")
    page = request.GET.get("page")
    per_page = request.GET.get("per_page", 20)
    if repo_id is None or page is None:
        return to_response(status=-1, response_type=HttpResponseBadRequest, msg="请求参数错误")
    try:
        page = int(page)
        per_page = int(per_page)
    except Exception:
        return to_response(status=-1, response_type=HttpResponseBadRequest, msg="请求参数错误")
    all_commits = Commit.objects.filter(repository_id=repo_id)
    if len(all_commits) == 0:
        return to_response(status=-1, response_type=HttpResponseBadRequest, msg="无此仓库")
    if per_page > 50:
        return to_response(status=-1, response_type=HttpResponseBadRequest, msg="per_page参数不得超过50")
    paginator = Paginator(all_commits, per_page)
    raw_page_commits = paginator.get_page(page)

    page_commits = {
        "has_next": raw_page_commits.has_next(),
        "commits": []
    }
    for commit in raw_page_commits:
        page_commits["commits"].append({
            "hash": commit.hash,
            "content": commit.content,
            "time": commit.time,
            "status": commit.status,
            "result_sa": commit.result_sa,
            "result_nlp": commit.result_nlp
        })
    return to_response(payload=page_commits)


"""
GET /api/file
[参数]
repo_id 仓库ID(由Load返回) [必须]
commit_hash Commit对应的Hash值 [必须]
[返回]
{"status": 0/-1, "msg": "...", "payload": 
    [
        # 其中各属性的含义见: info/models.py - File对象
        {"binary": true, "path": "..."},
        {"binary": false, "path": "...", "type": "...", "diff": "...", "arch_line": "..."}
        ...
    ]
}
"""
def get_file(request):
    repo_id = request.GET.get("repo_id")
    commit_hash = request.GET.get("commit_hash")
    brief_only = request.GET.get("brief_only", 0)
    if repo_id is None or commit_hash is None:
        return to_response(status=-1, response_type=HttpResponseBadRequest, msg="请求参数错误")
    try:
        commit = Commit.objects.get(repository_id=repo_id, hash=commit_hash)
    except Commit.DoesNotExist:
        return to_response(status=-1, response_type=HttpResponseBadRequest, msg="无此Commit")
    raw_files = File.objects.filter(commit=commit)
    files = []
    for file in raw_files:
        if file.binary:
            files.append({
                "binary": file.binary,
                "path": file.path
            })
        else:
            files.append({
                "binary": file.binary,
                "path": file.path,
                "type": file.type,
                "diff": file.diff if brief_only == 0 else None,
                "comments": file.comments,
                "content_new": file.content_new if brief_only == 0 else None,
                "content_old": file.content_old if brief_only == 0 else None,
                "arch_line": file.arch_line
            })
    return to_response(msg=commit.content, payload=files)